package com.leen.service.impl;

import com.leen.service.CrudService;
import org.springframework.data.repository.CrudRepository;

import java.util.Optional;

/**
 * 类说明
 *
 * @author admin
 * @date 2021-04-21
 */
public class CurdServiceImpl<T,ID> implements CrudService<T,ID> {

    //父(多态)
    private CrudRepository  crudRepository;

    /**
     * 预留给子类给我赋值
     * @param crudRepository
     */
    public void setCrudRepository(CrudRepository crudRepository) {
        this.crudRepository = crudRepository;
    }

    @Override
    public <S extends T> S save(S entity) {
        return (S) this.crudRepository.save(entity);
    }

    @Override
    public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
        return this.crudRepository.saveAll(entities);
    }

    @Override
    public Optional<T> findById(ID id) {
        return this.crudRepository.findById(id);
    }

    @Override
    public boolean existsById(ID id) {
        return this.crudRepository.existsById(id);
    }

    @Override
    public Iterable<T> findAll() {
        return this.crudRepository.findAll();
    }

    @Override
    public Iterable<T> findAllById(Iterable<ID> ids) {
        return this.crudRepository.findAllById(ids);
    }

    @Override
    public long count() {
        return this.crudRepository.count();
    }
}
